package com.njpi.stu.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njpi.stu.attendance.entity.CheckWorkLog;
import com.njpi.stu.attendance.entity.TotalData;
import com.njpi.stu.attendance.mapper.CheckWorkLogMapper;
import com.njpi.stu.attendance.service.ICheckWorkLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 考勤记录表 服务实现类
 * </p>
 *
 * @author pxl
 * @since 2019-11-07
 */
@Service
public class CheckWorkLogServiceImpl extends ServiceImpl<CheckWorkLogMapper, CheckWorkLog> implements ICheckWorkLogService {


    @Autowired
    private CheckWorkLogMapper checkWorkLogMapper;
    @Override
    public List<Long> selectAllCkeckId(String userName) {
        return checkWorkLogMapper.selectAllCkeckId(userName);
    }

    @Override
    public Boolean checkUserChecked(Long checkId, String userName) {
        boolean result = checkWorkLogMapper.checkUserChecked(checkId, userName) == null;
        return result;
    }

    /**
     * 根据用户名(学生名字)字查询报表数据
     * @param userName
     * @return
     */
    @Override
    public HashMap<String,TotalData> reportForStu(String userName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_name",userName);
        List<CheckWorkLog> checkWorkLogList = checkWorkLogMapper.selectList(queryWrapper);
        //汇总课程签到数
        HashMap<String,Integer> courseTotal=new HashMap();
        //每月的签到次数
        HashMap<String,Integer> monthTotal=new HashMap();
        Integer successApply=0;
        Integer failApply=0;
        for (CheckWorkLog checkWorkLog : checkWorkLogList) {
            String courseName = checkWorkLog.getCourseName();
            //统计签到成功和失败的总次数
            if(checkWorkLog.getType()>0){
                successApply+=1;
            }else {
                failApply+=1;
            }
            //统计所有课程的签到次数
            if(courseTotal.containsKey(courseName)){
                courseTotal.put(courseName,courseTotal.get(courseName)+1);
            }else {
                courseTotal.put(courseName,1);
            }

            //统计每月的签到次数（仅限今年）
            String month=(checkWorkLog.getCreateTime().getMonth()+1)+"";
            if(checkWorkLog.getCreateTime().getYear() == new Date().getYear()){
                if(monthTotal.containsKey(month)){
                    monthTotal.put(month,monthTotal.get(month)+1);
                }else {
                    monthTotal.put(month,1);
                }
            }
        }

        String[] courseNames=courseTotal.keySet().toArray(new String[courseTotal.keySet().size()]);
        Integer[] courseValues=courseTotal.values().toArray(new Integer[courseTotal.values().size()]);
        String[] months=monthTotal.keySet().toArray(new String[monthTotal.keySet().size()]);
        Integer[] monthValues=monthTotal.values().toArray(new Integer[monthTotal.values().size()]);

        TotalData courseTo=new TotalData(courseNames,courseValues);
        TotalData monthTo=new TotalData(months,monthValues);
        TotalData nuTo=new TotalData(successApply,failApply,courseNames.length);
        HashMap<String,TotalData> result=new HashMap<>();
        result.put("courseTo",courseTo);
        result.put("monthTo",monthTo);
        result.put("nuTo",nuTo);
        return result;
    }

    @Override
    public HashMap<String, TotalData> reportForTea(String userName) {
        return null;
    }
}
